package com.briup.jz.service.impl;


import com.briup.jz.bean.User;
import com.briup.jz.bean.extend.AccountEmployeeExtend;
import com.briup.jz.bean.extend.CommentExtend;
import com.briup.jz.bean.extend.UserExtend;
import com.briup.jz.dao.UserMapper;
import com.briup.jz.dao.extend.AccountEmployeeExtendMapper;
import com.briup.jz.dao.extend.CommentExtendMapper;
import com.briup.jz.dao.extend.UserExtendMapper;
import com.briup.jz.service.ICheckService;
import com.briup.jz.utils.CustomerException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author 马志宇
 * @project: jz
 * @description: 审核业务实现类
 * @create: 2020/6/13 17:19
 */
@Service
public class CheckServiceimpl implements ICheckService {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserExtendMapper userExtendMapper;
    @Resource
    private CommentExtendMapper commentExtendMapper;
    @Resource
    private AccountEmployeeExtendMapper accountEmployeeExtendMapper;

    /**
     * 1. 员工身份审核
     */
    // 通过状态查找所有员工
    @Override
    public List<UserExtend> queryEmployees_check(String stauts) {
        return userExtendMapper.queryEmployees_check(stauts);
    }

    // 通过id查找员工
    @Override
    public UserExtend findById(long id) {
        return userExtendMapper.selectById(id);
    }

    // 审核通过
    @Override
    public void pass_employee(long id) {
        userExtendMapper.checking(id, UserExtend.USERNORMAL);
    }

    // 审核不通过
    @Override
    public void fail_employee(long id) {
        userExtendMapper.checking(id, UserExtend.USERFORBIDDEN);
    }

    // 更新用户的状态
    @Override
    public void updateStatus_check(long id, String status) {
        // 先检查该id对应的用户是否存在
        User user = userMapper.selectByPrimaryKey(id);
        if(user == null){
            // 若不存在，报错！
            throw new CustomerException("修改状态失败，用户不存在");
        }
        //存在，更新
        userExtendMapper.updateStatus_check(id, status);
    }


    /**
     * 2. 提现审核
     */
    // 查找所有员工的交易
    @Override
    public List<AccountEmployeeExtend> findAllWithdrawal(String status) {
        return accountEmployeeExtendMapper.findAllWithdrawal(status);
    }

    // 检查员工是否已审核
    @Override
    public boolean checkstatus(long id) {
        // 1. 先查找该用户
        User userCheck = userMapper.selectByPrimaryKey(id);
        // 2. 判断该用户是否存在
        if(userCheck == null){
            // 当不存在，报错！
            throw new CustomerException("查询失败，要查询的数据不存在");
        }
        // 3. 检查员工状态是否为“用户正常”
        if(userCheck.getStatus().equals("用户正常")){
            //用户正常
            return true;
        }
        // 员工状态“未审核”或“用户禁用”
        return false;
    }

    // 检查是否余额 > 提现，若符合则计算提现后的余额
    @Override
    public long checkBalance(long id, long amount) {
        // 1. 先查找该用户的余额
        User userCheck = userMapper.selectByPrimaryKey(id);
        long balance = Long.valueOf(userCheck.getBalance()).longValue();
        // 2. 比较是否余额 > 提现
        if(balance > amount){
            //符合要求，返回提现后的余额
            long result = balance - amount;
            return result;
        }
        // 不符合要求，返回-1
        return -1;
    }

    // 审核此次提现（讲上面两个方法合并）
    @Override
    public long checkWithdrawal(long id, long amount) {
        // 1. 检查 员工是否已审核 和 是否余额>提现
        long result = checkBalance(id, amount);
        if(checkstatus(id) && (result > 0)){
            // 2. 符合体现要求，返回提现后的余额
            return result;
        }
        //不符合提现要求，返回-1
        return -1;
    }

    // 审核提现，更新交易的状态
    @Override
    public void updateStatus(long id, long amount) {
        long result = checkWithdrawal(id, amount);
        String status = result==-1? AccountEmployeeExtend.WITHDRAWAL_FAIL:AccountEmployeeExtend.WITHDRAWALED;
        accountEmployeeExtendMapper.updateStatus(id, status);
    }

    /**
     * 3. 仲裁
     */
    // 通过状态查找所有投诉
    @Override
    public List<CommentExtend> queryComplaints(String status) {
        return commentExtendMapper.queryComplaints(status);
    }
}
